﻿using ET.DBCommon.Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET.DBCommon.DAL
{
    public class PaperTestQuestionTaskStepDAL
    {
        public static List<PaperTestQuestionTaskStepModel> SelectTestQuestionTaskStepList()
        {
            string sql = "select * from testQuestionTaskStep";
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<PaperTestQuestionTaskStepModel> lst = new List<PaperTestQuestionTaskStepModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    PaperTestQuestionTaskStepModel info = GetTestQuestionTaskModel(dt.Rows[i]);
                    lst.Add(info);
                }
            }
            return lst;
        }

        public static List<PaperTestQuestionTaskStepModel> SelectBeforeStepList(int stepID)
        {
            string sql = "select * from testQuestionTaskStep ";
            if (stepID > 0)
                sql += "where TestQuestionTaskStepID < " + stepID;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<PaperTestQuestionTaskStepModel> lst = new List<PaperTestQuestionTaskStepModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    PaperTestQuestionTaskStepModel info = GetTestQuestionTaskModel(dt.Rows[i]);
                    lst.Add(info);
                }
            }
            return lst;
        }

        public static List<PaperTestQuestionTaskStepModel> SelectBeforeStepListByTask(int taskID)
        {
            string sql = "select * from testQuestionTaskStep ";
            if (taskID > 0)
                sql += "where TestQuestionTaskID < " + taskID;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<PaperTestQuestionTaskStepModel> lst = new List<PaperTestQuestionTaskStepModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    PaperTestQuestionTaskStepModel info = GetTestQuestionTaskModel(dt.Rows[i]);
                    lst.Add(info);
                }
            }
            return lst;
        }

        public static bool InsertTestQuestionTaskStep(out int newId, PaperTestQuestionTaskStepModel testQuestionTaskStep)
        {
            string sql = @"INSERT INTO `paper_testquestion_task_step`
                            (`TaskId`,
                            `OperateId`,
                            `Parameter`,
                            `Score`,
                            `SysType`,
                            `TaskType`,
                            `Describes`,
                            `DeviceTypeId`,
                            `DeviceId`,
                            `StepContent`,
                            `Prerequisite`,
                            `KeyWords`,
                            `TimeOut`,
                            `Duration`,
                            `IsContinued`,
                            `RoleId`,
                            `UIOrder`,
                            `MutexStepId`)
                            VALUES(@TaskId,@OperateId,@Parameter,@Score,@SysType,@TaskType,@Describes,
                            @DeviceTypeId,@DeviceId,@StepContent,@Prerequisite,@KeyWords,@TimeOut,@Duration,
                            @IsContinued,@RoleId,@UIOrder,@MutexStepId)";
            MySqlParameter[] parms = {
                new MySqlParameter("@TaskId", testQuestionTaskStep.TaskId ),
                new MySqlParameter("@OperateId", testQuestionTaskStep.OperateId),
                new MySqlParameter("@Parameter", testQuestionTaskStep.Parameter),
                new MySqlParameter("@Score", testQuestionTaskStep.Score),
                new MySqlParameter("@SysType", testQuestionTaskStep.SysType ),
                new MySqlParameter("@TaskType", testQuestionTaskStep.TaskType),
                new MySqlParameter("@Describes", testQuestionTaskStep.Describes),
                new MySqlParameter("@DeviceTypeId", testQuestionTaskStep.DeviceTypeId),
                new MySqlParameter("@DeviceId", testQuestionTaskStep.DeviceId ),
                new MySqlParameter("@StepContent", testQuestionTaskStep.StepContent),
                new MySqlParameter("@Prerequisite", testQuestionTaskStep.Prerequisite),
                new MySqlParameter("@KeyWords", testQuestionTaskStep.KeyWords),
                new MySqlParameter("@TimeOut", testQuestionTaskStep.TimeOut),
                new MySqlParameter("@Duration", testQuestionTaskStep.Duration),
                new MySqlParameter("@IsContinued", testQuestionTaskStep.IsContinued == "True"? "1" : "0"),
                new MySqlParameter("@RoleId", testQuestionTaskStep.RoleId),
                 new MySqlParameter("@UIOrder", testQuestionTaskStep.UIOrder),
                 new MySqlParameter("@MutexStepId",testQuestionTaskStep.IsMutexStep ? 1 : 0),
            };

            return DbHelper.ExecuteNonQuery(out newId, sql, parms) > 0;
        }

        public static bool InsertTestQuestionTaskSteps(PaperTestQuestionTaskModel testQuestionTask)
        {
            string sql = "insert into testquestiontaskstep (Id,StepID,Parameter,score) (select @TestQuestionTaskID,StepID,'{}',0 from taskstepitem where TaskID=@TaskID)";
            MySqlParameter[] parms = {
                new MySqlParameter("@TestQuestionTaskID", testQuestionTask.TestId ),
                //new MySqlParameter("@TaskID", testQuestionTask.TaskID)
            };

            return DbHelper.ExecuteNonQuery(sql, parms) != -1;
        }
        //没有这个MutexStep字段
        public static bool UpdateMuteStep(String mute, PaperTestQuestionTaskStepModel testQuestionTaskStep)
        {
            // String sql = "UPDATE testquestiontaskstep SET MutexStep= "+ mute + " WHERE TestQuestionTaskStepID= "+ testQuestionTaskStep .TestQuestionTaskStepID;
            // DbHelper.ExecuteDataset(sql);
            return true;
        }

        public static bool UpdateTestQuestionTaskStep(PaperTestQuestionTaskStepModel testQuestionTaskStep)
        {
            string sql = @"UPDATE `paper_testquestion_task_step`
                            SET
                            `TaskId` = @TaskId,
                            `OperateId` = @OperateId,
                            `Parameter` = @Parameter,
                            `Score` = @Score,
                            `SysType` = @SysType,
                            `TaskType` = @TaskType,
                            `Describes` = @Describes,
                            `DeviceTypeId` = @DeviceTypeId,
                            `DeviceId` = @DeviceId,
                            `StepContent` = @StepContent,
                            `Prerequisite` = @Prerequisite,
                            `KeyWords` = @KeyWords,
                            `TimeOut`=@TimeOut,
                            `Duration`=@Duration,
                            `IsContinued`=@IsContinued,
                            `RoleId`=@RoleId,
                            `UIOrder`=@UIOrder,
                            `MutexStepId`=@MutexStepId

                            WHERE `Id` = @Id ";
            //ljz注释 2023.7.11
            //MutexStep = @MutexStep,
            MySqlParameter[] parms = {
                 new MySqlParameter("@Id", testQuestionTaskStep.Id ),
               new MySqlParameter("@TaskId", testQuestionTaskStep.TaskId ),
                new MySqlParameter("@OperateId", testQuestionTaskStep.OperateId),
                new MySqlParameter("@Parameter", testQuestionTaskStep.Parameter),
                new MySqlParameter("@Score", testQuestionTaskStep.Score),
                new MySqlParameter("@SysType", testQuestionTaskStep.SysType ),
                new MySqlParameter("@TaskType", testQuestionTaskStep.TaskType),
                new MySqlParameter("@Describes", testQuestionTaskStep.Describes),
                new MySqlParameter("@DeviceTypeId", testQuestionTaskStep.DeviceTypeId),
                new MySqlParameter("@DeviceId", testQuestionTaskStep.DeviceId ),
                new MySqlParameter("@StepContent", testQuestionTaskStep.StepContent),
                new MySqlParameter("@Prerequisite", testQuestionTaskStep.Prerequisite),
                new MySqlParameter("@KeyWords", testQuestionTaskStep.KeyWords),
                new MySqlParameter("@TimeOut", testQuestionTaskStep.TimeOut),
                new MySqlParameter("@Duration", testQuestionTaskStep.Duration),
                new MySqlParameter("@IsContinued", testQuestionTaskStep.IsContinued == "True"? "1" : "0"),
                new MySqlParameter("@RoleId", testQuestionTaskStep.RoleId),
                new MySqlParameter("@UIOrder", testQuestionTaskStep.UIOrder),
                new MySqlParameter("@MutexStepId", testQuestionTaskStep.IsMutexStep ? 1 : 0)
            };
            return DbHelper.ExecuteNonQuery(sql, parms) > 0;
        }

        public static bool DeleteTestQuestionTaskStep(int Id)
        {
            string sql = "delete from paper_testquestion_task_step WHERE Id = @Id";
            MySqlParameter[] parms = {
                new MySqlParameter("@Id", Id)
            };
            return DbHelper.ExecuteNonQuery(sql, parms) > 0;
        }

        public static bool DeleteTestQuestionTaskSteps(int TaskId)
        {
            string sql = "delete from paper_testquestion_task_step WHERE TaskId = @TaskId";
            MySqlParameter[] parms = {
                new MySqlParameter("@TaskId", TaskId)
            };
            return DbHelper.ExecuteNonQuery(sql, parms) != -1;
        }

        public static List<PaperTestQuestionTaskStepModel> GetStepByTestQuestionTaskID(int TaskId)
        {
            string sql = "select * from paper_testquestion_task_step where TaskId = " + TaskId;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<PaperTestQuestionTaskStepModel> lst = new List<PaperTestQuestionTaskStepModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    PaperTestQuestionTaskStepModel info = GetTestQuestionTaskModel(dt.Rows[i]);
                    lst.Add(info);
                }
            }
            return lst;
        }
        public static List<PaperTestQuestionTaskStepModel> GetStepDesListByQuestionID(int testQuestionID)
        {
            string sql = "select paper_testquestion_task.Id ,paper_testquestion_task.`Describes` from paper_testquestion_task inner join paper_testquestion_task_step on paper_testquestion_task_step.TaskId = paper_testquestion_task.Id " +
                "inner join paper_testquestion on paper_testquestion.Id = paper_testquestion_task.TestId where paper_testquestion_task_step.TaskType =" + "'" + "任务" + "'and paper_testquestion.Id = " + testQuestionID;
            DataSet ds = DbHelper.ExecuteDataset(sql);

            List<PaperTestQuestionTaskStepModel> lst = new List<PaperTestQuestionTaskStepModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    PaperTestQuestionTaskStepModel info = GetMiniTestQuestionTaskModel(dt.Rows[i]);
                    lst.Add(info);
                }
            }
            return lst;
        }


        private static PaperTestQuestionTaskStepModel GetTestQuestionTaskModel(DataRow dr)
        {
            PaperTestQuestionTaskStepModel info = new PaperTestQuestionTaskStepModel();
            info.Id = Convert.ToInt32(dr["Id"]);
            info.TaskId = Convert.ToInt32(dr["TaskId"]);
            if (dr["OperateId"] != DBNull.Value)
                info.OperateId = Convert.ToInt32(dr["OperateId"]);
            info.Parameter = dr["Parameter"].ToString();
            if (dr["Score"] != DBNull.Value)
                info.Score = decimal.Parse(dr["Score"].ToString());
            if (dr["SysType"] != DBNull.Value)
                info.SysType = dr["SysType"].ToString();
            if (dr["TaskType"] != DBNull.Value)
                info.TaskType = dr["TaskType"].ToString();
            info.Describes = dr["Describes"].ToString();
            if (dr["DeviceTypeId"] != DBNull.Value)
                info.DeviceTypeId = Convert.ToInt32(dr["DeviceTypeId"].ToString());
            if (dr["DeviceId"] != DBNull.Value)
                info.DeviceId = dr["DeviceId"].ToString();
            info.StepContent = dr["StepContent"].ToString();
            if (dr["Prerequisite"] != DBNull.Value)
                info.Prerequisite = dr["Prerequisite"].ToString();
            info.KeyWords = dr["KeyWords"].ToString();
            if (dr["TimeOut"] != DBNull.Value)
                info.TimeOut = Convert.ToInt32(dr["TimeOut"].ToString());
            if (dr["Duration"] != DBNull.Value)
                info.Duration = decimal.Parse(dr["Duration"].ToString());
            if (dr["IsContinued"] != DBNull.Value)
                info.IsContinued = dr["IsContinued"].ToString() == "1" ? "True" : "False";
            if (dr["RoleId"] != DBNull.Value)
                info.RoleId = dr["RoleId"].ToString();
            if (dr["UIOrder"] != DBNull.Value)
                info.UIOrder = Convert.ToInt32(dr["UIOrder"].ToString());
            if (dr["MutexStepId"] != DBNull.Value)
            {
                info.IsMutexStep = Convert.ToInt32(dr["MutexStepId"].ToString()) == 1;
            }
            return info;
        }
        private static PaperTestQuestionTaskStepModel GetMiniTestQuestionTaskModel(DataRow dr)
        {
            PaperTestQuestionTaskStepModel info = new PaperTestQuestionTaskStepModel();
            info.Id = Convert.ToInt32(dr["Id"]);
            info.Describes = dr["Describes"].ToString();
            return info;
        }
    }
}
